<?php
require_once LIBS_DIR . '../config/dbconfig.php'; //LIBS_DIR is defined in config.php

class Database 
{
	private $hConn = FALSE;   //connection handler
	private $debug = FALSE;   //if true you will see db errors
	protected $result = FALSE;
	protected $dbprefix = '';

	
	protected function fetch()
	{
		return mysql_fetch_assoc($this->result);
	}
	
	protected function __construct() 
	{	
		$this->debug = DBDEBUG;
		$this->dbprefix = DBPREFIX;
		
		$this->hConn = @mysql_connect(DBHOST, DBUSER, DBPASS);
		if (!is_resource($this->hConn))
		{
			$this->debug ? die("Cann't connect to database server") : exit();
		}
		else
		{
			if (!@mysql_select_db(DBNAME, $this->hConn))
			{
				$this->debug ? die("Cann't connect to database") : exit();
			}
			@mysql_query("SET NAMES 'utf8'", $this->hConn);
		}
	}
	
	protected function query($query)
	{
		$this->result = @mysql_query($query, $this->hConn);
		if (!($this->result))
		{
			$this->debug ? die("Error in query: " . mysql_error() . "<br>Query: <b><pre>$query</pre></b>") : exit();
		}
	} 
	
	protected function __destruct()
	{
		if (is_resource($this->hConn)){
			mysql_close($this->hConn); 
		}
	}
}
?>